﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace System.Linq
{
    public abstract class ExpressionBuilderInclude : ExpressionBuilder
    {
        private static readonly ConcurrentDictionary<Type, MethodInfo> TypeMethodInfos = new ConcurrentDictionary<Type, MethodInfo>();

        private static readonly MethodInfo MatchInfo = typeof(Enumerable).GetMethods().First((MethodInfo x) => x.Name == "Contains" && x.GetParameters().Length == 2);

        protected override bool IsEmpty(object value)
        {
            return base.IsEmpty(value) || value.GetType().BaseType != typeof(Array) || ((Array)value).Length == 0;
        }

        protected static MethodInfo GetMethodInfo(Type type)
        {
            if (!TypeMethodInfos.TryGetValue(type, out MethodInfo methodInfo))
            {
                methodInfo = MatchInfo.MakeGenericMethod(new Type[] { type });
                TypeMethodInfos.TryAdd(type, methodInfo);
            }
            return methodInfo;
        }
    }
}